package com.jcoinexchange.web.entity;

import com.jcoinexchange.web.util.StringFormatUtil;

import java.util.Objects;

/**
 * 返回结果类
 * @param <T>
 */
public class BasicResponse<T> {

    private Integer code;

    private String message;

    private T data;

    public BasicResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

        public static <T> BasicResponse<T> success(T data) {
        return new BasicResponse<>(ResponseBaseCode.SUCCESS.getCode(), ResponseBaseCode.SUCCESS.getMsg(), data);
    }

    public static  <T> BasicResponse<T> error(ResponseCode code) {
        return new BasicResponse<>(code.getCode(), code.getMsg(), null);
    }


    public static <T> BasicResponse<T>  error(int code, String errorMsg) {
        return new BasicResponse<>(code, errorMsg, null);
    }

    public static <T> BasicResponse<T>  error(ResponseCode code, String format, Object... arg) {
        String formatResult;
        if(Objects.isNull(arg) || arg.length == 0){
            formatResult = format;
        }else{
            formatResult = StringFormatUtil.format(format, arg);
        }
        return new BasicResponse<>(code.getCode(), formatResult, null);
    }

    public static  BasicResponse<?> success() {
        return new BasicResponse<>(ResponseBaseCode.SUCCESS.getCode(), ResponseBaseCode.SUCCESS.getMsg(), null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
